package com.qf.mq;

import com.qf.model.StandardSubmit;
import com.qf.netty4.NettyClient;
import com.qf.netty4.Utils.Command;
import com.qf.netty4.Utils.MsgUtils;
import com.qf.netty4.entity.CmppMessageHeader;
import com.qf.netty4.entity.CmppSubmit;
import com.qf.netty4.entity.ControlWindow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class ReceiveSmsFromMq {

    @Autowired
    private NettyClient nettyClient;

    /**
     * 消息接受  并发消费
     */
    @RabbitListener(queues = "${gateway.sendtopic}", containerFactory = "pointTaskContainerFactory")
    public void receive(StandardSubmit submit) throws IOException, InterruptedException {
        Thread.sleep(10);
        log.info("接收消息:{}",submit);
        String mobile=submit.getDestMobile();
        String content=submit.getMessageContent();
        String srcNumber = submit.getSrcNumber();
        int sequenceId = MsgUtils.getSequence();
        CmppMessageHeader submitMessage = new CmppSubmit(Command.CMPP2_VERSION, "", srcNumber, sequenceId, mobile, content);
        ControlWindow controlWindow = ControlWindow.getInstance();
        log.info("enter window seqID:{},submit:{}",sequenceId,submit);
        controlWindow.put(sequenceId,submit);
        nettyClient.submit(submitMessage);
    }


}
